# Archlive <http://archlive-pkg.googlecode.com>

pkgname=kernel26-git
pkgver=20090903
pkgrel=1
_realpkgname=kernel26
_configure_as_ARCH_way=y
_kernelname=${_realpkgname#kernel26}
## if you want your own version, such as 2.6.31-archlive, then 
## "_configure_as_ARCH_way" set to "1" and
## "_realpkgname" change to "kernel26-archlive"
_include_aufs2=y
_include_firmware=y
_keep_build_dir=y
_include_squashfs=y
##
pkgdesc="The Linux Kernel and modules from Linus' git tree with Gentoo patches, aufs2 and reiserfs4 file system patch, and kernel Chinese characters patches. Firmware included. Linux内核及模块快照版，包含内核中文字符支持，Gentoo官方补丁、aufs2补丁、reiser4补丁。包含Firmware."
url="http://www.kernel.org/"
arch=(i686 x86_64)
license=('GPL2')
provides=('kernel26=2.6.31' 'aufs' 'kernel26-firmware=2.6.31')
conflicts=('kernel24' 'kernel24-scsi' 'kernel26-scsi' 'kernel26' 'kernel26-firmware' 'unionfs' 'aufs')
depends=('coreutils' 'module-init-tools' 'mkinitcpio>=0.5.15')
makedepends=('git' 'gcc')
backup=(etc/mkinitcpio.d/kernel26.preset)
install=$pkgname.install

source=(kernel26.preset config.{i686,x86_64} \
        logo_linux_{clut224.ppm,mono.pbm,vga16.ppm}
	## reiserfs4 patch
	http://www.kernel.org/pub/linux/kernel/people/edward/reiser4/reiser4-for-2.6/reiser4-for-2.6.30.patch.gz
	## Gentoo patches
	http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4100_dm-bbr.patch
	http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4202_fbcondecor-0.9.6.patch
	http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4400_alpha-sysctl-uac.patch
	### Chinese Characters Patches 孙海勇 大侠的 内核中文补丁  http://youbest.cublog.cn/
	#http://zdbr.net.cn/download/utf8-kernel-2.6.29-core-1.patch.bz2
	utf8-kernel-2.6.29-core-1.patch
	http://zdbr.net.cn/download/utf8-kernel-2.6-fonts-2.patch.bz2
	http://zdbr.net.cn/download/utf8-kernel-2.6.28-fbcondecor-1.patch.bz2
)
md5sums=()
extrapatch() {
  msg2 "Add Gentoo patches 加入Gentoo官方基本补丁..."
  for i in $(ls $srcdir/4[0-9][0-9][0-9]*);do
    	echo "Applying $(basename $i)" 
  	patch -Np1 -i $i || return 1
  done

  msg2 "Add the kernel Chinese characters patches 加入孙海勇 大侠的 内核中文补丁  http://youbest.cublog.cn/"
  patch -Np1 -i $srcdir/utf8-kernel-2.6.29-core-1.patch || return 1
  patch -Np1 -i $srcdir/utf8-kernel-2.6.28-fbcondecor-1.patch || return 1
  patch -Np1 -i $srcdir/utf8-kernel-2.6-fonts-2.patch || return 1

  msg2 "Adding reiserfs4 patch reiser4-for-2.6.30.patch"
  patch -Np1 -i ${srcdir}/reiser4-for-2.6.30.patch || return 1

  if [ "${_include_aufs2}" = "y" ]; then 
	msg2 "Adding aufs2 standalone patch from git tree"
	[ -d $srcdir/aufs2-standalone ] && rm -rf $srcdir/aufs2-standalone
	git clone $startdir/aufs2-standalone $srcdir/aufs2-standalone
	git checkout origin/aufs2
	cp -r $srcdir/aufs2-standalone/{Documentation,fs,include,aufs2-base.patch,aufs2-kbuild.patch,aufs2-standalone.patch} \
  		$srcdir/$_gitname-build/
	cd $srcdir/$_gitname-build
	patch -Np1 -i aufs2-kbuild.patch || return 1
	patch -Np1 -i aufs2-base.patch || return 1
	patch -Np1 -i aufs2-standalone.patch || return 1
  fi
}


_gitroot="git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git"
_gitname="linux-2.6.git"


####################################################################
# KERNEL CONFIG FILES
#
# This PKGBUILD searches for config files in the current directory
# and will use the first one it finds from the following
# list as base configuration:
# 	config.local
# 	config.saved.$CARCH
# 	config.$CARCH
#
####################################################################

#############################################################
# PATCHES
#
# This package builds the vanilla git kernel by default,
# but it is possible to patch the source without modifying
# this PKGBUILD.
#
# Simply create a directory 'patches' in your PKGBUILD
# directory and _any_ file (dotfiles excluded) in this
# folder will be applied to the kernel source.
#
# Prefixing the patch file names with dots will obviously
# excluded them from the patching process.
#
#############################################################


#############################
# CONFIGURATION
#
# Uncomment desired options
#############################


#######
# Set to menuconfig, xconfig, gconfig or oldconfig
#
# If you set it to something else, the
# (manual) kernel configuration step will
# be skipped
#
_menu_method="menuconfig"	


#######
# The directory where the kernel should be built
#
# Can be useful, for example, if you want to compile on a
# tmpfs mount, which can speed up the compilation process
#
_build_dir="$srcdir"


#######
# Stop build process after kernel configuration
#
# This option enables _save_config implicitly.
#
# _configure_only=1


#######
# Save the .config file to package directory
# as config.saved.$CARCH
#
# _save_config=1


#######
# Make the kernel build process verbose
#
# _verbose=1

##############################
# where the magic happens...
##############################

build() {
	local _karch="x86"

	#################################
	# Get the latest kernel sources
	#################################
	msg "Fetching sources..."

	cd "$startdir"
	if [ -d "$_gitname" ]; then
		msg2 "Updating sources..."
		cd "$_gitname" && git fetch && cd "$OLDPWD" || return 1
	else
		msg2 "Cloning the project..."
		git clone --mirror "$_gitroot" "$_gitname" || return 1
	fi

	msg "Creating build branch..."
	rm -rf "$_build_dir/$_gitname-build"
	git clone "$_gitname" "$_build_dir/$_gitname-build" || return 1

	msg "Fetch the aufs2-standalone git tree..."
	if [ -d $startdir/aufs2-standalone ]; then
		cd $startdir/aufs2-standalone
		git pull origin
	else
		git clone http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-standalone.git $startdir/aufs2-standalone || return 1
	fi

	cd "$_build_dir/$_gitname-build"

	# Add Gentoo patches, Fedora patches, reiserfs4 patch and kernel Chinese characters patches.
	extrapatch

	####################################
	# Add Archlinux logo to the source
	####################################
	msg "Adding Archlinux logo..."
	cp "$srcdir/logo_linux_clut224.ppm"  drivers/video/logo/ &&
	cp "$srcdir/logo_linux_mono.pbm"     drivers/video/logo/ &&
	cp "$srcdir/logo_linux_vga16.ppm"    drivers/video/logo/ || return 1


	#################
	# Apply patches
	#################
	if [ -d "$startdir/patches" ]; then
		msg "Applying patches..."
		for i in "$startdir/patches/"*; do
			msg2 "Applying ${i##*/}..."
			patch -Np1 -i "$i" || (error "Applying ${i##*/} failed" && return 1)
		done
	fi

	#################
	# CONFIGURATION
	#################

	#########################
	# Loading configuration
	#########################
	msg "Loading configuration..."
	for i in local "saved.$CARCH" "$CARCH"; do
		if [ -e "$startdir/config.$i" ]; then
			msg2 "Using kernel config file config.$i..."
			cp -f "$startdir/config.$i" .config || return 1
			break
		fi
	done

	[ ! -e .config ] &&
		warning "No suitable kernel config file was found. You'll have to configure the kernel from scratch."

	###########################
	# Configure as ARCH way
	###########################
	if [ "$_configure_as_ARCH_way" = "y" ]; then
		msg "Configure the kernel version as ARCH way, like 2.6.31-ARCH"
		if [ "${_kernelname}" != "" ]; then local_version=${_kernelname}; else local_version="-ARCH"; fi
		sed -i "s|^EXTRAVERSION = .*$|EXTRAVERSION =|g" Makefile
		sed -i "s|.*CONFIG_LOCALVERSION=.*|CONFIG_LOCALVERSION=\"${local_version}\"|g" .config
		sed -i "s|.*CONFIG_LOCALVERSION_AUTO.*|\# CONFIG_LOCALVERSION_AUTO is not set|g" .config
	fi
	# The default .config without support for squashfs, can change it by set "_include_squashfs" as "y".
	if [ "$_include_squashfs" = "y" ]; then
		msg "Selected squashfs4 included in kernel package."
		if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" = "x" ]; then
			sed -i "s|.*CONFIG_SQUASHFS.*|CONFIG_SQUASHFS=m\
				\n# CONFIG_SQUASHFS_EMBEDDED is not set\
				\nCONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE=3|g" ./.config
		fi
	else
		if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" != "x" ]; then
			sed -i "s|CONFIG_SQUASHFS=m|\# CONFIG_SQUASHFS is not set|g" .config
		fi
	fi
	if [ "${_include_aufs2}" = "y" ]; then 
		msg "Selected aufs2 included in kernel package."
		if [ "x$(grep "CONFIG_AUFS_FS=m" .config)" = "x" ]; then
			sed -i "/.*CONFIG_NILFS2_FS.*/ a\
				\CONFIG_AUFS_FS=m\
				\n# CONFIG_AUFS_BRANCH_MAX_127 is not set\
				\n# CONFIG_AUFS_BRANCH_MAX_511 is not set\
				\nCONFIG_AUFS_BRANCH_MAX_1023=y\
				\n# CONFIG_AUFS_BRANCH_MAX_32767 is not set\
				\nCONFIG_AUFS_HINOTIFY=y\
				\nCONFIG_AUFS_EXPORT=y\
				\nCONFIG_AUFS_RDU=y\
				\nCONFIG_AUFS_SHWH=y\
				\nCONFIG_AUFS_BR_RAMFS=y\
				\nCONFIG_AUFS_BR_FUSE=y\
				\n# CONFIG_AUFS_DEBUG is not set\
				\nCONFIG_AUFS_BDEV_LOOP=y\
				\nCONFIG_AUFS_POLL=y" ./.config
		fi
	else
		if [ "x$(grep "CONFIG_AUFS_FS=m" .config)" != "x" ]; then
			sed -i "s|CONFIG_AUFS_FS=m|\# CONFIG_AUFS_FS is not set|g" .config
		fi
	fi
	make prepare
	msg "Update .config Finished"

	###########################
	# Start the configuration
	###########################
	msg "Updating configuration..."

	yes "" | make config > /dev/null

	##############################################
	# Save the config file the package directory
	##############################################
	if [[ -n "$_save_config" || -n "$_configure_only" ]]; then
		msg "Saving configuration..."
		msg2 "Saving $_build_dir/$_gitname-build/.config as $startdir/config.saved.$CARCH"
		cp .config "$startdir/config.saved.$CARCH" || return 1
	fi


	#######################################
	# Stop after configuration if desired
	#######################################
	if [ -n "$_configure_only" ]; then
		rm -rf "$_build_dir/$_gitname-build"
		return 1
	fi

	#################
	# BUILD PROCESS
	#################

	######################
	# Get kernel version
	######################
	_kernver="$(make kernelrelease)"

	if [ "$_configure_as_ARCH_way" = "y" ]; then
		# Check the _kernver again, if it is your wanted (Special for _configure_as_ARCH_way)
		_localversion=$(grep CONFIG_LOCALVERSION= .config | cut -d "\"" -f 2)
		if [ "${_kernver#*-}" != ${_localversion#-} ]; then
			msg "There are some errors when get your own kernel version."
			msg "_kernver=${_kernver}, _localversion=${_localversion}."
			exit 1
		else
			msg "Get the right kernel version as ${_kernver}"
		fi
	fi	

	################################
	# Build the kernel and modules
	################################
	msg "Building kernel and modules..."
	make V="$_verbose" bzImage modules || return 1


	################
	# INSTALLATION
	################

	#####################
	# Install the image
	#####################
	msg "Installing kernel image..."
	install -D -m644 System.map                "$pkgdir/boot/System.map26" &&
	install -D -m644 arch/$_karch/boot/bzImage "$pkgdir/boot/vmlinuz26" &&
	install -D -m644 .config                   "$pkgdir/boot/kconfig26"    || return 1


	##########################
	# Install kernel modules
	##########################
	msg "Installing kernel modules..."
	make INSTALL_MOD_PATH="$pkgdir" modules_install


	##############################
	# Install fake kernel source
	##############################
	install -D -m644 Module.symvers  "$pkgdir/usr/src/linux-$_kernver/Module.symvers"  &&
	install -D -m644 Makefile        "$pkgdir/usr/src/linux-$_kernver/Makefile"        &&
	install -D -m644 kernel/Makefile "$pkgdir/usr/src/linux-$_kernver/kernel/Makefile" &&
	install -D -m644 .config         "$pkgdir/usr/src/linux-$_kernver/.config"         &&
	install -D -m644 .config         "$pkgdir/lib/modules/$_kernver/.config"           || return 1


	#######################################################
	# Install scripts directory and fix permissions on it
	#######################################################
	cp -a scripts "$pkgdir/usr/src/linux-$_kernver" &&
		chmod og-w -R "$pkgdir/usr/src/linux-$_kernver" || return 1


	##########################
	# Install header files
	##########################
	msg "Installing header files..."

	# kernel headers
	msg2 "kernel"
	for i in acpi asm-{generic,$_karch} config linux math-emu media net pcmcia scsi sound trace video; do
		mkdir -p "$pkgdir/usr/src/linux-$_kernver/include/$i" &&
			cp -a include/$i "$pkgdir/usr/src/linux-$_kernver/include" || return 1
	done

	# lirc headers
	msg2 "lirc"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/drivers/media/video" &&
		cp drivers/media/video/*.h "$pkgdir/usr/src/linux-$_kernver/drivers/media/video/" || return 1

	for i in bt8xx cpia2 cx25840 cx88 em28xx et61x251 pwc saa7134 sn9c102 usbvideo zc0301; do
		mkdir -p "$pkgdir/usr/src/linux-$_kernver/drivers/media/video/$i" &&
			cp -a drivers/media/video/$i/*.h "$pkgdir/usr/src/linux-$_kernver/drivers/media/video/$i" || return 1
	done

	# md headers
	msg2 "md"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/drivers/md" &&
		cp drivers/md/*.h "$pkgdir/usr/src/linux-$_kernver/drivers/md" || return 1

	# inotify.h
	msg2 "inotify.h"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/include/linux" &&
		cp include/linux/inotify.h "$pkgdir/usr/src/linux-$_kernver/include/linux/" || return 1

	# CLUSTERIP file for iptables
	msg2 "CLUSTERIP file for iptables"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/net/ipv4/netfilter/" &&
		cp net/ipv4/netfilter/ipt_CLUSTERIP.c "$pkgdir/usr/src/linux-$_kernver/net/ipv4/netfilter/" || return 1

	# wireless headers
	msg2 "wireless"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/net/mac80211/" &&
		cp net/mac80211/*.h "$pkgdir/usr/src/linux-$_kernver/net/mac80211/" || return 1

	# Kconfig files
	msg2 "Kconfig files"
	for i in $(find . -name "Kconfig*"); do
		mkdir -p "$pkgdir/usr/src/linux-$_kernver/${i%/*}" &&
			cp "$i" "$pkgdir/usr/src/linux-$_kernver/$i" || return 1
	done


	########################################
	# Install architecture dependent files
	########################################
	msg "Installing architecture files..."
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/arch/$_karch/kernel" &&
		cp arch/$_karch/kernel/asm-offsets.s "$pkgdir/usr/src/linux-$_kernver/arch/$_karch/kernel"

	cp arch/$_karch/Makefile* "$pkgdir/usr/src/linux-$_kernver/arch/$_karch"
	cp -r arch/$_karch/configs "$pkgdir/usr/src/linux-$_kernver/arch/$_karch"

	# copy arch includes for external modules and fix the nVidia issue
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/arch/$_karch" &&
		cp -a "arch/$_karch/include" "$pkgdir/usr/src/linux-$_kernver/arch/$_karch/" || return 1

	# create a necessary symlink to the arch folder
	cd "$pkgdir/usr/src/linux-$_kernver/arch" || return 1

	if [ "$CARCH" = "x86_64" ]; then
		ln -s $_karch x86_64 || return 1
	else
		ln -s $_karch i386 || return 1
	fi

	cd "$OLDPWD" || return 1


	################################
	# Remove unneeded architecures
	################################
	msg "Removing unneeded architectures..."
	for i in "$pkgdir/usr/src/linux-$_kernver/arch/"*; do
		[ "${i##*/}" != "$_karch" ] && rm -rf "$i"
	done


	##################################
	# Create some important symlinks
	##################################
	msg "Creating important symlinks..."
	cd "$pkgdir/usr/src/linux-$_kernver/include" &&
		ln -s asm-$_karch asm &&
		cd "$OLDPWD" || return 1

	cd "$pkgdir/lib/modules/$_kernver" &&
		rm -rf source build &&
		ln -s ../../../usr/src/linux-$_kernver build &&
		cd "$OLDPWD" || return 1

	###################
	# Fix permissions
	###################
	msg "Fixing permissions..."
	chown -R root:root "$pkgdir/usr/src/linux-$_kernver" &&
		find "$pkgdir/usr/src/linux-$_kernver" -type d -exec chmod 755 {} \; || return 1


	############################
	# Install mkinitcpio files
	############################
	msg "Installing preset file..."
	install -D -m644 "$srcdir/kernel26.preset" \
		"$pkgdir/etc/mkinitcpio.d/kernel26.preset" || return 1

	msg "Generating kernel26.kver..."
	echo -e "# DO NOT EDIT THIS FILE\nALL_kver='$_kernver'" \
		> "$pkgdir/etc/mkinitcpio.d/kernel26.kver" || return 1


	if [ "$_include_firmware" != "y" ]; then
		#######################
		# Remove the firmware
		#######################
		rm -rf "$pkgdir/lib/firmware"
	fi
	if [ "$_keep_build_dir" != "y" ]; then
		##########################
		# Remove build directory
		##########################
		rm -rf "$_build_dir/$_gitname-build"
	fi
}